package com.kanshu.ksgb.fastread.doudou.common.business.ad.toutiao

import android.app.Activity
import android.graphics.Color
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.bytedance.sdk.openadsdk.*
import com.kanshu.ksgb.fastread.doudou.R
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdStyleBean
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdUtils
import com.kanshu.ksgb.fastread.doudou.common.business.ad.BaseAdListener
import com.kanshu.ksgb.fastread.doudou.common.business.ad.SplashAdListener
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter
import com.kanshu.ksgb.fastread.doudou.common.business.ad.retrofit.ADConfigBean
import com.kanshu.ksgb.fastread.doudou.common.business.ad.utils.ThreadPool
import com.kanshu.ksgb.fastread.doudou.common.business.utils.UserUtils
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils
import com.kanshu.ksgb.fastread.doudou.common.util.Utils
import com.makeramen.roundedimageview.RoundedImageView
import io.reactivex.android.schedulers.AndroidSchedulers


/**
 * Title:
 * Description:
 * Copyright: Copyright(c)2018
 * Company: 看书网
 * CreateTime: 2018/8/3 15:55
 *
 * @author EvanHe
 * @version 3.0
 */
const val AD_TIME_OUT = 3000
const val AD_SPLASH_TT = "ad_splash_tt"

class AdTouTiaoUtils {
    companion object {

        private fun getAdNative(activity: Activity): TTAdNative {
            return TTAdManagerHolder.getInstance(activity.applicationContext).createAdNative(activity.applicationContext)
        }

        fun fetchSplashAd(activity: Activity, container: ViewGroup?, skipContainer: View?, adConfig: ADConfigBean, adListener: BaseAdListener?) {
            skipContainer?.visibility = View.GONE
            val adContainer = container ?: FrameLayout(activity)
            val adWidth = DisplayUtils.getScreenWidth(activity)
            val adHeight = DisplayUtils.getScreenHeight(activity) - activity.resources.getDimension(R.dimen.px_335)
            val adSlot = AdSlot.Builder()
                    .setCodeId(adConfig.ad_position_id)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(adWidth, adHeight.toInt())
                    .build()
            getAdNative(activity).loadSplashAd(adSlot, object : TTAdNative.SplashAdListener {
                override fun onSplashAdLoad(ad: TTSplashAd?) {
                    if (ad == null) {
                        adListener?.onAdLoadFailed()
                        return
                    }
                    val submit = ThreadPool.submit(AndroidSchedulers.mainThread(), {
                        adListener?.onADClosed()
                    }, AD_TIME_OUT.toLong())

                    ad.let {
                        AdUtils.destroyAd(adContainer)
                        adContainer.removeAllViews()
                        adContainer.addView(it.splashView)
                        adContainer.tag = AD_SPLASH_TT
                        adListener?.onAdLoadSucceeded(it.splashView)
                        if (adListener is SplashAdListener) {
                            adListener.isShowSkipView(false)
                        }
                        //设置不开启开屏广告倒计时功能以及不显示跳过按钮
                        //ad.setNotAllowSdkCountdown();
                        it.setSplashInteractionListener(object : TTSplashAd.AdInteractionListener {
                            override fun onAdClicked(p0: View?, p1: Int) {
                                Log.i("toutiao_splash", "onAdClicked ")
                                AdUtils.pVUVAd(AdPresenter.AD_CLICK, adConfig)
                                adListener?.onAdClicked()
                                if (adListener is SplashAdListener) {
                                    adListener.onJump(true)
                                }
                            }

                            override fun onAdSkip() {
                                Log.i("toutiao_splash", "onAdSkip ")
                                adListener?.onADClosed()
                            }

                            override fun onAdShow(view: View?, type: Int) {
                                submit?.dispose()
                                Log.i("toutiao_splash", "onAdShow ")
                                AdUtils.pVUVAd(AdPresenter.AD_SHOW, adConfig)
                            }

                            override fun onAdTimeOver() {
                                submit?.dispose()
                                Log.i("toutiao_splash", "onAdTimeOver ")
                                adListener?.onADClosed()
                            }
                        })
                    }
                }

                override fun onTimeout() {
                    Log.i("toutiao_splash", "adLoadTimeout")
                    adListener?.onAdLoadFailed()
                }

                override fun onError(code: Int, message: String?) {
                    Log.i("toutiao_splash", "onError code: $code  message: $message")
                    adListener?.onAdLoadFailed()
                }

            }, AD_TIME_OUT)
        }


        /*   fun fetchBannerAd(activity: Activity, container: ViewGroup?, adConfig: ADConfigBean, adStyle: Int, layout: Int, adListener: BaseAdListener?) {
               val adContainer = container ?: FrameLayout(activity)
               try {
                   TTAdManagerHolder.getInstance(activity).requestPermissionIfNecessary(activity)
               } catch (e: Exception) {
                   e.printStackTrace()
               }
               //val adWidth = DisplayUtils.getScreenWidth(activity)
               //val adHeight = DisplayUtils.dip2px(activity, 60f)
               val adSlot = AdSlot.Builder()
                       .setCodeId(adConfig.ad_position_id)
                       .setSupportDeepLink(true)
                       .setImageAcceptedSize(600, 150)
                       .build()
               getAdNative(activity).loadBannerAd(adSlot, object : TTAdNative.BannerAdListener {
                   override fun onBannerAdLoad(ad: TTBannerAd?) {
                       Log.i("toutiao_banner", "onBannerAdLoad")
                       ad?.let {
                           val bannerView = it.bannerView ?: return
                           it.setSlideIntervalTime(30 * 1000)
                           adContainer.visibility = View.VISIBLE
                           adContainer.removeAllViews()
                           adContainer.addView(bannerView)
                           it.setBannerInteractionListener(object : TTBannerAd.AdInteractionListener {
                               override fun onAdClicked(view: View?, type: Int) {
                                   Log.i("toutiao_banner", "onAdClicked")
                                   AdUtils.pVUVAd(AdPresenter.AD_CLICK, adConfig)
                                   adListener?.onAdClicked()
                               }

                               override fun onAdShow(view: View?, type: Int) {
                                   Log.i("toutiao_banner", "onAdShow")
                                   AdUtils.pVUVAd(AdPresenter.AD_SHOW, adConfig)
                                   adListener?.onAdLoadSucceeded(adContainer)
                               }
                           })
                           //在banner中显示网盟提供的dislike icon
                           it.setShowDislikeIcon(object : TTAdDislike.DislikeInteractionCallback {
                               override fun onSelected(position: Int, value: String) {
                                   Log.i("toutiao_banner", "onSelected")
                                   adContainer.visibility = View.GONE
                                   adListener?.onADClosed()
                               }

                               override fun onCancel() {
                                   adListener?.onADClosed()
                                   Log.i("toutiao_banner", "onCancel")
                               }
                           })
                       }
                   }

                   override fun onError(code: Int, message: String?) {
                       Log.i("toutiao_banner", "onError code: $code  message: $message")
                       //adListener?.onAdLoadFailed()
                       AdUtils.fetchRenderAd(activity, adContainer, null, adConfig, adStyle, layout, adListener)
                   }
               })
           }*/

        fun fetchNativeBannerAd(activity: Activity, container: ViewGroup?, adConfig: ADConfigBean, adStyle: Int, layout: Int, adListener: BaseAdListener?) {
            val adContainer: ViewGroup = container ?: FrameLayout(activity)
            val adSlot = AdSlot.Builder()
                    .setCodeId(adConfig.ad_position_id)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(600, 257)
                    .setNativeAdType(AdSlot.TYPE_BANNER) //请求原生广告时候，请务必调用该方法，设置参数为TYPE_BANNER或TYPE_INTERACTION_AD
                    .setAdCount(1)
                    .build()
            //大图：宽高比1.78，最低尺寸690*388  小图：宽高比1.52，最低尺寸228*150 组图：宽高比1.52，最低尺寸228*150
            getAdNative(activity).loadNativeAd(adSlot, object : TTAdNative.NativeAdListener {
                override fun onNativeAdLoad(ads: MutableList<TTNativeAd>?) {
                    Log.i("toutiao_native_banner", "onNativeAdLoad")
                    if (Utils.isEmptyList(ads)) {
                        AdUtils.fetchRenderAd(activity, adContainer, adConfig, adStyle, layout, adListener)
                        return
                    }
                    ads!![0].let {
                        Log.i("toutiao_native_banner", " imageMode ${it.imageMode}")
                        val bannerView: View = LayoutInflater.from(activity).inflate(R.layout.layout_tt_ad_native_banner, adContainer, false)
                        when (adConfig.ad_position.toInt()) {
                            ADConfigBean.POSITION_BOOK_READER_BOTTOM, ADConfigBean.POSITION_BOOK_READER_BOTTOM_5 -> {
                                bannerView.setBackgroundColor(Color.TRANSPARENT)
                            }
                            else -> {
                                bannerView.setBackgroundColor(Color.WHITE)
                            }
                        }
                        val adImg: ImageView = bannerView.findViewById(R.id.ad_image)
                        val title: TextView = bannerView.findViewById(R.id.ad_title)
                        val desc: TextView = bannerView.findViewById(R.id.ad_desc)
                        val adClose: ImageView = bannerView.findViewById(R.id.ad_close)
                        bindDislikeAction(activity, it, adClose, adContainer, adListener)
                        title.text = it.title ?: adConfig.title ?: ""
                        desc.text = it.description ?: adConfig.description ?: ""
                        if (!Utils.isEmptyList(it.imageList)) {
                            val height = DisplayUtils.dip2px(activity, 50f)
                            val width: Double
                            width = when (it.imageMode) {
                                TTAdConstant.IMAGE_MODE_GROUP_IMG, TTAdConstant.IMAGE_MODE_SMALL_IMG -> height * 1.52
                                TTAdConstant.IMAGE_MODE_LARGE_IMG -> height * 1.78
                                else -> height * 1.52
                            }
                            val layoutParams = FrameLayout.LayoutParams(width.toInt(), height)
                            adImg.layoutParams = layoutParams
                            val image = it.imageList[0]
                            if (image != null && image.isValid) {
                                GlideImageLoader.load(image.imageUrl, adImg, GlideImageLoader.getEmptyConfig())
                            }
                        }
                        AdUtils.destroyAd(adContainer)
                        adContainer.removeAllViews()
                        adContainer.addView(bannerView)
                        adListener?.onAdLoadSucceeded(adContainer)
                        //可以被点击的view, 也可以把nativeView放进来意味整个广告区域可被点击
                        val clickViewList = ArrayList<View>()
                        clickViewList.add(adContainer)

                        //触发创意广告的view（点击下载或拨打电话）
                        // val creativeViewList = ArrayList<View>()
                        //如果需要点击图文区域也能进行下载或者拨打电话动作，请将图文区域的view传入
                        //creativeViewList.add(adContainer)
                        //重要! 这个涉及到广告计费，必须正确调用。convertView必须使用ViewGroup。
                        var isClicked = false
                        it.registerViewForInteraction(adContainer, clickViewList, clickViewList, adClose, object : TTNativeAd.AdInteractionListener {
                            override fun onAdClicked(view: View, ad: TTNativeAd?) {
                                if (isClicked) return@onAdClicked
                                isClicked = true
                                ad?.run {
                                    Log.i("toutiao_native_banner", "onAdClicked")
                                    onAdClicked(activity, adContainer, adConfig, adStyle, layout, adListener)
                                }
                            }

                            override fun onAdCreativeClick(view: View, ad: TTNativeAd?) {
                                if (isClicked) return@onAdCreativeClick
                                isClicked = true
                                ad?.run {
                                    Log.i("toutiao_native_banner", "onAdCreativeClick")
                                    onAdClicked(activity, adContainer, adConfig, adStyle, layout, adListener)
                                }
                            }

                            override fun onAdShow(ad: TTNativeAd?) {
                                ad?.run {
                                    Log.i("toutiao_native_banner", "onAdShow")
                                    AdUtils.pVUVAd(AdPresenter.AD_SHOW, adConfig)
                                }
                            }
                        })
                    }
                }

                override fun onError(code: Int, message: String?) {
                    Log.i("toutiao_native_banner", "onError code: $code  message: $message")
                    AdUtils.fetchRenderAd(activity, adContainer, adConfig, adStyle, layout, adListener)
                }
            })
        }

        fun fetchFeedAd(activity: Activity, container: ViewGroup?, adConfig: ADConfigBean, adStyle: Int, layout: Int, adListener: BaseAdListener?) {
            val adContainer: ViewGroup = container ?: FrameLayout(activity)
            try {
                TTAdManagerHolder.getInstance(activity).requestPermissionIfNecessary(activity)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            val adSlot = AdSlot.Builder()
                    .setCodeId(adConfig.ad_position_id)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(640, 320)
                    .setAdCount(1)
                    .build()
            getAdNative(activity).loadFeedAd(adSlot, object : TTAdNative.FeedAdListener {
                override fun onFeedAdLoad(ads: MutableList<TTFeedAd>?) {
                    Log.i("toutiao_feed", "onFeedAdLoad")
                    if (Utils.isEmptyList(ads)) return
                    ads!![0].let {
                        var height = 0.0
                        val width = DisplayUtils.getScreenWidth(activity) - DisplayUtils.dip2px(activity, 28f)
                        //信息流广告的样式，有大图、小图、组图和视频，通过ad.getImageMode()来判断
                        val view = when (it.imageMode) {
                            TTAdConstant.IMAGE_MODE_GROUP_IMG -> {
                                View.inflate(activity, R.layout.layout_tt_ad_group_pic, null) as View
                            }
                            TTAdConstant.IMAGE_MODE_SMALL_IMG -> {
                                View.inflate(activity, R.layout.layout_tt_ad_small_pic, null) as View
                            }
                            TTAdConstant.IMAGE_MODE_LARGE_IMG -> {
                                height = width * (9 / 16.0)
                                View.inflate(activity, R.layout.layout_tt_ad_large_pic, null) as View
                            }
                            TTAdConstant.IMAGE_MODE_VIDEO -> {
                                height = width * (9 / 16.0)
                                View.inflate(activity, R.layout.layout_tt_ad_large_video, null) as View
                            }
                            else -> return@onFeedAdLoad
                        }
                        val title = view.findViewById(R.id.ad_title) as TextView
                        val desc = view.findViewById(R.id.ad_desc) as TextView?
                        val source = view.findViewById(R.id.ad_source) as TextView?
                        val icon = view.findViewById(R.id.ad_icon) as RoundedImageView?
                        val adImg = view.findViewById(R.id.ad_image) as ImageView?
                        val adImg1 = view.findViewById(R.id.ad_image1) as ImageView?
                        val adImg2 = view.findViewById(R.id.ad_image2) as ImageView?
                        val videoLayout = view.findViewById(R.id.ad_video) as FrameLayout?

                        title.text = it.title ?: adConfig.title ?: ""
                        desc?.text = it.description ?: adConfig.description ?: ""
                        if (!TextUtils.equals(it.title, it.source)) {
                            source?.text = it.source ?: ""
                        }
                        if (icon != null && it.icon != null && it.icon.isValid) {
                            icon.visibility = View.VISIBLE
                            GlideImageLoader.load(it.icon.imageUrl, icon, GlideImageLoader.getEmptyConfig())
                        }
                        when (it.imageMode) {
                            TTAdConstant.IMAGE_MODE_GROUP_IMG -> {
                                Log.i("toutiao_feed", "组图类型广告：${it.imageList[1]?.imageUrl}")
                                if (!Utils.isEmptyList(it.imageList)) {
                                    if (it.imageList.size > 2) {
                                        val image: TTImage = it.imageList[0]
                                        val image1: TTImage = it.imageList[1]
                                        val image2: TTImage = it.imageList[2]
                                        if (adImg != null && image.isValid) {
                                            GlideImageLoader.load(image.imageUrl, adImg, GlideImageLoader.getEmptyConfig())
                                        }
                                        if (adImg1 != null && image1.isValid) {
                                            GlideImageLoader.load(image1.imageUrl, adImg1, GlideImageLoader.getEmptyConfig())
                                        }
                                        if (adImg2 != null && image2.isValid) {
                                            GlideImageLoader.load(image2.imageUrl, adImg2, GlideImageLoader.getEmptyConfig())
                                        }
                                    }
                                }
                            }
                            TTAdConstant.IMAGE_MODE_SMALL_IMG -> {
                                Log.i("toutiao_feed", "小图类型广告： ${it.imageList[0]?.imageUrl}")
                                if (!Utils.isEmptyList(it.imageList)) {
                                    val image: TTImage = it.imageList[0]
                                    if (adImg != null && image.isValid) {
                                        GlideImageLoader.load(image.imageUrl, adImg, GlideImageLoader.getEmptyConfig())
                                    }
                                }
                            }
                            TTAdConstant.IMAGE_MODE_LARGE_IMG -> {
                                Log.i("toutiao_feed", "大图类型广告： ${it.imageList[0]?.imageUrl}")
                                if (!Utils.isEmptyList(it.imageList)) {
                                    height = width * (9 / 16.0)
                                    val layoutParams = FrameLayout.LayoutParams(width, height.toInt())
                                    adImg?.layoutParams = layoutParams
                                    val image: TTImage = it.imageList[0]
                                    if (adImg != null && image.isValid) {
                                        GlideImageLoader.load(image.imageUrl, adImg, GlideImageLoader.getEmptyConfig())
                                    }
                                }
                            }
                            TTAdConstant.IMAGE_MODE_VIDEO -> {
                                Log.i("toutiao_feed", "视频类型广告： ${it.imageList[0]?.imageUrl}")
                                videoLayout?.run {
                                    if (height > 0) {
                                        val lp = FrameLayout.LayoutParams(width, height.toInt())
                                        layoutParams = lp
                                    }
                                    val video = it.adView
                                    if (video != null && video.parent == null) {
                                        removeAllViews()
                                        addView(video)
                                    }
                                }
                            }
                        }
                        AdUtils.destroyAd(adContainer)
                        adContainer.removeAllViews()
                        adContainer.addView(view)
                        adListener?.onAdLoadSucceeded(adContainer)
                        val clickViewList = ArrayList<View>()
                        clickViewList.add(adContainer)
                        var isClicked = false
                        //重要! 这个涉及到广告计费，必须正确调用。convertView必须使用ViewGroup。
                        it.registerViewForInteraction(adContainer, clickViewList, clickViewList, object : TTNativeAd.AdInteractionListener {
                            override fun onAdClicked(view: View, ad: TTNativeAd?) {
                                if (isClicked) return@onAdClicked
                                isClicked = true
                                ad?.run {
                                    Log.i("toutiao_feed", "onAdClicked")
                                    onAdClicked(activity, adContainer, adConfig, adStyle, layout, adListener)
                                }
                            }

                            override fun onAdCreativeClick(view: View, ad: TTNativeAd?) {
                                if (isClicked) return@onAdCreativeClick
                                isClicked = true
                                ad?.run {
                                    Log.i("toutiao_feed", "onAdCreativeClick")
                                    onAdClicked(activity, adContainer, adConfig, adStyle, layout, adListener)
                                }
                            }

                            override fun onAdShow(ad: TTNativeAd?) {
                                ad?.run {
                                    Log.i("toutiao_feed", "onAdShow")
                                    AdUtils.pVUVAd(AdPresenter.AD_SHOW, adConfig)
                                }
                            }
                        })
                    }
                }

                override fun onError(code: Int, message: String?) {
                    Log.i("toutiao_feed", "onError code: $code  message: $message")
                    //adListener?.onAdLoadFailed()
                    AdUtils.fetchRenderAd(activity, adContainer, adConfig, adStyle, layout, adListener)
                }
            })
        }

        /**
         * 加载激励视频广告
         */
        fun fetchRewardedVideoAd(activity: Activity, container: ViewGroup?, adConfig: ADConfigBean, adListener: BaseAdListener?) {
            val adContainer: ViewGroup = container ?: FrameLayout(activity)
            try {
                TTAdManagerHolder.getInstance(activity).requestPermissionIfNecessary(activity)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            val adSlot: AdSlot = AdSlot.Builder()
                    .setCodeId(adConfig.ad_position_id)
                    .setSupportDeepLink(true)
                    .setAdCount(1)
                    .setImageAcceptedSize(1080, 1920)
                    .setRewardName("金币") //奖励的名称
                    .setRewardAmount(0)   //奖励的数量
                    //必传参数，表来标识应用侧唯一用户；若非服务器回调模式或不需sdk透传
                    //可设置为空字符串
                    .setUserID(UserUtils.getUserId())
                    .setOrientation(TTAdConstant.VERTICAL)  //设置期望视频播放的方向，为TTAdConstant.HORIZONTAL或TTAdConstant.VERTICAL
                    .setMediaExtra("media_extra") //用户透传的信息，可不传
                    .build()
            getAdNative(activity).loadRewardVideoAd(adSlot, object : TTAdNative.RewardVideoAdListener {
                //视频广告的素材加载完毕，比如title,视频url等，不包括视频文件,在此回调后，可以播放在线视频，网络不好可能出现加载缓冲，影响体验。
                override fun onRewardVideoAdLoad(videoAd: TTRewardVideoAd?) {
                    if (videoAd == null) {
                        return
                    }
                    adListener?.onBackAd(videoAd)
                    Log.i("toutiao_video", "rewardVideoAd loaded")
                    //视频播放完成后，奖励验证回调，rewardVerify：是否有效，rewardAmount：奖励梳理，rewardName：奖励名称
                    videoAd.setRewardAdInteractionListener(object : TTRewardVideoAd.RewardAdInteractionListener {
                        override fun onRewardVerify(rewardVerify: Boolean, rewardAmount: Int, rewardName: String?) {
                            Log.i("toutiao_video", "verify: $rewardVerify  amount: $rewardAmount  name: $rewardName")
                            adListener?.onAdLoadSucceeded(adContainer)
                        }

                        override fun onAdShow() {
                            Log.i("toutiao_video", "rewardVideoAd show")
                            AdUtils.pVUVAd(AdPresenter.AD_SHOW, adConfig)
                        }

                        override fun onAdVideoBarClick() {
                            Log.i("toutiao_video", "rewardVideoAd bar click")
                            adListener?.onAdClicked()
                            AdUtils.pVUVAd(AdPresenter.AD_CLICK, adConfig)
                        }

                        override fun onVideoComplete() {
                            Log.i("toutiao_video", "rewardVideoAd complete")
                        }

                        override fun onAdClose() {
                            Log.i("toutiao_video", "rewardVideoAd close")
                        }
                    })
                }

                //视频广告加载后的视频文件资源缓存到本地的回调
                override fun onRewardVideoCached() {
                    Log.i("toutiao_video", "rewardVideoAd video cached")
                }

                override fun onError(code: Int, message: String?) {
                    Log.i("toutiao_video", "onError code: $code  message: $message")
                    //adListener?.onAdLoadFailed()
                    //AdUtils.fetchRenderAd(activity, adContainer, null, adConfig, adStyle, layout, adListener)
                }
            })
        }

        /**
         * 加载全屏视频广告
         */
        fun fetchFullScreenVideoAd(activity: Activity, container: ViewGroup?, adConfig: ADConfigBean, adListener: BaseAdListener?) {
            val adContainer: ViewGroup = container ?: FrameLayout(activity)
            try {
                TTAdManagerHolder.getInstance(activity).requestPermissionIfNecessary(activity)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            val adSlot: AdSlot = AdSlot.Builder()
                    .setCodeId(adConfig.ad_position_id)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(1080, 1920)
                    .setOrientation(TTAdConstant.VERTICAL)
                    .build()
            getAdNative(activity).loadFullScreenVideoAd(adSlot, object : TTAdNative.FullScreenVideoAdListener {
                override fun onFullScreenVideoAdLoad(mttFullVideoAd: TTFullScreenVideoAd?) {
                    if (mttFullVideoAd == null) {
                        return
                    }
                    adListener?.onBackAd(mttFullVideoAd)
                    Log.i("toutiao_full_video", "FullVideoAd loaded")
                    mttFullVideoAd.setFullScreenVideoAdInteractionListener(object : TTFullScreenVideoAd.FullScreenVideoAdInteractionListener {
                        override fun onSkippedVideo() {
                            Log.i("toutiao_full_video", "FullVideoAd skipped")
                            adListener?.onAdLoadSucceeded(adContainer)
                        }

                        override fun onAdShow() {
                            Log.i("toutiao_full_video", "FullVideoAd show")
                            AdUtils.pVUVAd(AdPresenter.AD_SHOW, adConfig)
                        }

                        override fun onAdVideoBarClick() {
                            Log.i("toutiao_full_video", "FullVideoAd bar click")
                            adListener?.onAdClicked()
                            AdUtils.pVUVAd(AdPresenter.AD_CLICK, adConfig)
                        }

                        override fun onVideoComplete() {
                            Log.i("toutiao_full_video", "FullVideoAd complete")
                        }

                        override fun onAdClose() {
                            Log.i("toutiao_full_video", "FullVideoAd close")
                        }

                    })

                }

                override fun onFullScreenVideoCached() {
                    Log.i("toutiao_full_video", "FullVideoAd video cached")
                }

                override fun onError(code: Int, message: String?) {
                    Log.i("toutiao_full_video", "onError code: $code  message: $message")
                }
            })

        }

        fun onAdClicked(activity: Activity, adContainer: ViewGroup?, adConfig: ADConfigBean, adStyle: Int, layout: Int, adListener: BaseAdListener?) {
            adListener?.onAdClicked()
            AdUtils.pVUVAd(AdPresenter.AD_CLICK, adConfig)
            if (AdStyleBean.SPLASH_STYLE == adStyle) {
                return
            }
            AdUtils.pVUVAd(AdPresenter.AD_REQUEST, adConfig)
            when (adStyle) {
                AdStyleBean.BANNER_STYLE -> fetchNativeBannerAd(activity, adContainer, adConfig, adStyle, layout, adListener)
                AdStyleBean.IMAGE_TEXT_STYLE -> fetchFeedAd(activity, adContainer, adConfig, adStyle, layout, adListener)
            }
        }

        //接入网盟的dislike 逻辑，有助于提示广告精准投放度
        private fun bindDislikeAction(activity: Activity, ad: TTNativeAd, dislikeView: View, adConfigBean: ViewGroup, adListener: BaseAdListener?) {
            val ttAdDislike = ad.getDislikeDialog(activity)
            ttAdDislike?.setDislikeInteractionCallback(object : TTAdDislike.DislikeInteractionCallback {
                override fun onSelected(position: Int, value: String) {
                    adConfigBean.removeAllViews()
                    adListener?.onADClosed()
                }

                override fun onCancel() {
                }
            })
            dislikeView.setOnClickListener { ttAdDislike!!.showDislikeDialog() }
        }
    }
}